package com.sgb.kjwl.view.ui.fragment.pick;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickDeviceBinding;
import com.sgb.kjwl.model.entity.PickDeviceEntity;
import com.sgb.kjwl.view.ui.activity.pick.PickProjectActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickDeviceAdapter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

@Deprecated
public class PickDeviceFragment extends AppFragment {

    private FragmentPickDeviceBinding binding;
    private PickProjectActivity activity;
    private PickDeviceAdapter adapter;
    private boolean isLoadMore;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_device, container, false);
        EventBus.getDefault().register(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        adapter = new PickDeviceAdapter(activity, null);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                binding.recyclerView.setLoadingMoreEnabled(true);
                isLoadMore = false;
                activity.getDevice(false);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                activity.getDevice(true);

            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof PickProjectActivity) {
            activity = (PickProjectActivity) context;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(List<PickDeviceEntity> data) {
        if (data.get(0) instanceof PickDeviceEntity) {
            showList(data);
            binding.llTitle.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String s) {
        if (s.equals("DEVICE_EMPTY")) {
            if (isLoadMore) {
                binding.recyclerView.loadMoreComplete();
                binding.recyclerView.setLoadingMoreEnabled(false);
            } else {
                binding.recyclerView.refreshComplete();
                binding.llTitle.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setLoadingMoreEnabled(true);
            }
        }
    }

    private void showList(List<PickDeviceEntity> data) {
        if (isLoadMore) {
            adapter.addDatas(data);
            binding.recyclerView.loadMoreComplete();
        } else {
            adapter.setDatas(data);
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
